﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiteMapEditor.Forms.WebRessources;

namespace SiteMapEditor.Controls
{
    public partial class AreaControl : UserControl, ISiteMapSavable
    {
        private Dictionary<string, string> collec;

        private string initialUrl = "";
        private string initialId = "";
        private string initialIcon = "";
        private string initialTitle = "";
        private string initialDescription = "";
        private bool initialShowGroups = false;

        #region Delegates

        public delegate void SaveEventHandler(object sender, SaveEventArgs e);

        #endregion

        #region Event Handlers

        public event SaveEventHandler Saved;

        #endregion

        public AreaControl()
        {
            InitializeComponent();

            this.collec = new Dictionary<string, string>();

            ToolTip tip = new ToolTip();
            tip.ToolTipTitle = "Information";
            tip.SetToolTip(this.txtAreaIcon, "Specifies a URL to a 16x16 pixel image.");
            tip.SetToolTip(this.txtAreaId, "Specifies a unique identifier in ASCII. Spaces are not allowed.\r\n\r\nValid values:a-z, A-Z, 0-9, and underscore (_).");
            tip.SetToolTip(this.txtAreaUrl, "Specifies the Microsoft Dynamics CRM for Outlook URL to render for the Outlook folder that represents the Area.");
            tip.SetToolTip(this.chkAreaShowGroups, "Control whether groups of sub-areas are shown in the navigation pane.");
            tip.SetToolTip(this.txtAreaTitle, "Deprecated. Use the <Titles> (SiteMap) and <Title> (SiteMap) elements.");
            tip.SetToolTip(this.txtAreaDescription, "Deprecated. Use the <Description> (SiteMap) element.");
        }

        public AreaControl(Dictionary<string, string> collection):this()
        {
            this.collec = collection;

            this.FillControls();
        }

        private void FillControls()
        {
            this.txtAreaUrl.Text = collec.ContainsKey("Url") ? collec["Url"] : "";
            this.txtAreaId.Text = collec.ContainsKey("Id") ? collec["Id"] : "";
            this.txtAreaIcon.Text = collec.ContainsKey("Icon") ? collec["Icon"] : "";
            this.chkAreaShowGroups.Checked = collec.ContainsKey("ShowGroups") && collec["ShowGroups"].ToLower() == "true";
            this.txtAreaResourceId.Text = collec.ContainsKey("ResourceId") ? collec["ResourceId"] : "";
            this.txtAreaDescriptionResourceId.Text = collec.ContainsKey("DescriptionResourceId") ? collec["DescriptionResourceId"] : "";

            this.txtAreaTitle.Text = collec.ContainsKey("Title") ? collec["Title"] : "";
            this.txtAreaDescription.Text = collec.ContainsKey("Description") ? collec["Description"] : "";

            this.initialUrl = this.txtAreaUrl.Text;
            this.initialId = this.txtAreaId.Text;
            this.initialIcon = this.txtAreaIcon.Text;
            this.initialShowGroups = this.chkAreaShowGroups.Checked;
            this.initialDescription = this.txtAreaDescription.Text;
            this.initialTitle = this.txtAreaTitle.Text;
        }

        private void SiteMapControl_Leave(object sender, EventArgs e)
        {
            if (this.initialUrl != this.txtAreaUrl.Text ||
                this.initialId != this.txtAreaId.Text ||
                this.initialIcon != this.txtAreaIcon.Text ||
                this.initialDescription != this.txtAreaDescription.Text ||
                this.initialTitle != this.txtAreaTitle.Text ||
                this.initialShowGroups != this.chkAreaShowGroups.Checked)
            {
                if (MessageBox.Show("You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.Save();
                }
            }
        }

        public void Save()
        {
            if (this.txtAreaId.Text.Length == 0)
            {
                MessageBox.Show(this, "Id is required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Dictionary<string, string> collection = new Dictionary<string, string>();
                if (this.txtAreaUrl.Text.Length > 0)
                    collection.Add("Url", this.txtAreaUrl.Text);
                if (this.txtAreaId.Text.Length > 0)
                    collection.Add("Id", this.txtAreaId.Text);
                if (this.txtAreaIcon.Text.Length > 0)
                    collection.Add("Icon", this.txtAreaIcon.Text);
                if (this.txtAreaResourceId.Text.Length > 0)
                    collection.Add("ResourceId", this.txtAreaResourceId.Text);
                if (this.txtAreaDescriptionResourceId.Text.Length > 0)
                    collection.Add("DescriptionResourceId", this.txtAreaDescriptionResourceId.Text);
                if (this.txtAreaDescription.Text.Length > 0)
                    collection.Add("Description", this.txtAreaDescription.Text);
                if (this.txtAreaTitle.Text.Length > 0)
                    collection.Add("Title", this.txtAreaTitle.Text);
                collection.Add("ShowGroups", this.chkAreaShowGroups.Checked.ToString().ToLower());

                this.initialUrl = this.txtAreaUrl.Text;
                this.initialId = this.txtAreaId.Text;
                this.initialIcon = this.txtAreaIcon.Text;
                this.initialShowGroups = this.chkAreaShowGroups.Checked;
                this.initialDescription = this.txtAreaDescription.Text;
                this.initialTitle = this.txtAreaTitle.Text;

                this.SendSaveMessage(collection);
            }
        }

        #region Send Events

        /// <summary>
        /// Sends a connection success message 
        /// </summary>
        /// <param name="service">IOrganizationService generated</param>
        /// <param name="parameters">Lsit of parameter</param>
        private void SendSaveMessage(Dictionary<string, string> collection)
        {
            SaveEventArgs sea = new SaveEventArgs() { AttributeCollection = collection };

            if (this.Saved != null)
            {
                this.Saved(this, sea);
            }
        }

        #endregion

        private void btnBrowsIcon_Click(object sender, EventArgs e)
        {
            WebResourcePicker wrp = new WebResourcePicker(WebResourcePicker.WebResourceType.Image);
            wrp.StartPosition = FormStartPosition.CenterParent;

            if (wrp.ShowDialog() == DialogResult.OK)
            {
                this.txtAreaIcon.Text = "$webresource:" + wrp.SelectedResource;
            }
        }

        private void buttonBrowseUrl_Click(object sender, EventArgs e)
        {
            WebResourcePicker wrp = new WebResourcePicker(WebResourcePicker.WebResourceType.WebPage);
            wrp.StartPosition = FormStartPosition.CenterParent;

            if (wrp.ShowDialog() == DialogResult.OK)
            {
                this.txtAreaUrl.Text = "$webresource:" + wrp.SelectedResource;
            }
        }
    }
}
